package com.tomorrow.analyze;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.res.Resources;

import com.tomorrow.weather.application.WeatherApplication;
import com.tomorrow.weather.entity.City;
import com.tomorrow.weather.mInterface.IHotCityData;
import com.tomorrow.weather.ui.R;

public class HotCityDataDom implements IHotCityData
{

	@Override
	public List<City> getHotCity()
	{
		ArrayList<City> hotCities = new ArrayList<City>();
		InputStream is = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try
		{
			DocumentBuilder builder = factory.newDocumentBuilder();
			Resources resources = (WeatherApplication.getContext())
					.getResources();
			is = resources.openRawResource(R.raw.hot_city);
			Document document = builder.parse(is);
			Element root = (Element) document.getDocumentElement();
			NodeList cityList = root.getChildNodes();
			for (int i = 0; i < cityList.getLength(); i++)
			{

				Element item = (Element) cityList.item(i);
				String codeId = item.getAttribute("code");
				System.out.println(codeId);
				String cityName = item.getFirstChild().getNodeValue();
				City city = new City(codeId, cityName);
				System.out.println(cityName);
				hotCities.add(city);
			}
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			if (is != null)
			{
				try
				{
					is.close();
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}

		return hotCities;
	}

}
